<?php
/*------------------------------------------------------------------------
# com_ilike - XIPAT Like Article Component
# version: 1.0
# ------------------------------------------------------------------------
# author    Duong Tien Dung - www.xipat.com
# copyright Copyright (C) 2013 www.xipat.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.xipat.com
# Technical Support:  http://www.xipat.com/support.html
-------------------------------------------------------------------------*/
// no direct access

defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * Methods supporting a list of Xplikes records.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_ilike
 * @since		1.6
 */
class ilikeModelXplikes extends JModelList
{
	/**
	 * Constructor.
	 *
	 * @param	array	An optional associative array of configuration settings.
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', '#__xplike.id',
				'state', '#__xplike.state',
				'content_id', '#__xplike.content_id', 'likes', '#__xplike.likes', 'component', '#__xplike.component', 'dislikes', '#__xplike.dislikes'
			);
		}
		parent::__construct($config);
	}
	
	
	
	#FUNCTION_GET_AUTHOR#

	/**
	 * Build an SQL query to load the list data.
	 *
	 * @return	JDatabaseQuery
	 * @since	1.6
	 */
	protected function getListQuery()
	{
		// Initialise variables.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);

		// Select the required fields from the table.
		$query->select(
			$this->getState(
				'list.select',
				'#__xplike.id, #__xplike.state, #__xplike.checked_out AS checked_out, #__xplike.checked_out_time AS checked_out_time, 
				#__xplike.publish_up, #__xplike.publish_down, #__xplike.ordering
				, #__xplike.content_id, #__xplike.likes, #__xplike.component, #__xplike.dislikes, c.title, c.hits, c.created as publish_date'
			)
		);
		$query->from('`#__xplike`');

		// Join over the users for the checked out user.
		$query->select('uc.name AS editor');
		$query->join('LEFT', '#__users AS uc ON uc.id=#__xplike.checked_out');
		$query->join('LEFT', '#__content AS c ON c.id=#__xplike.content_id');
	
		// Filter by search
        $search = $this->getState('filter.search');
        if (!empty($search)) {
            if (stripos($search, 'id:') === 0) {
                $query->where('#__xplike.id = '.(int) substr($search, 3));
            } else {
                $search = $db->Quote('%'.$db->escape($search, true).'%');
                $query->where('(c.title LIKE '.$search.' OR #__xplike.id LIKE '.$search.')');
            }
        }
         //end search
        // Filter on the component. 
        $component = $this->getState('filter.component');
        if (!empty($component)) {
            $query->where('#__xplike.component LIKE "%'.$component.'%"');
        }  		
		// Add the list ordering clause.
		$orderCol	= $this->state->get('list.ordering');
		$orderDirn	= $this->state->get('list.direction');
		
		$query->order($db->escape($orderCol.' '.$orderDirn));

		//echo nl2br(str_replace('#__','jos_',$query));
		return $query;
	}

	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param	type	The table type to instantiate
	 * @param	string	A prefix for the table class name. Optional.
	 * @param	array	Configuration array for model. Optional.
	 * @return	JTable	A database object
	 * @since	1.6
	 */
	public function getTable($type = 'Xplikes', $prefix = 'ilikeTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app = JFactory::getApplication('administrator');

		// Load the filter state.
		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
							$this->setState('filter.search', $search);
        $component = $this->getUserStateFromRequest($this->context . 'filter.component', 'filter_component');
        $this->setState('filter.component', $component);                             
		
		// List state information.
		parent::populateState('#__xplike.id', 'DESC');
	}
	public function getStatus($from,$to)
	{
		$db = JFactory::getDbo();
		$sql="SELECT * FROM #__xplike_log WHERE created > '".$from."' AND created < '".$to."'";
		$db->setQuery($sql);
		$result=$db->loadObjectList();
		$data->like=0;
		$data->dislike=0;
		$data->content=0;
		$contentarray= array();
		foreach ($result as $row)
		{
			if ($row->value == 1) $data->like++;
			else $data->dislike++;
			if (!in_array($row->content_id,$contentarray)) 
			{
				$data->content++;
				$contentarray[]=$row->content_id;	
			}
		}
		return $data;
	}
    public function getDaylast1()
    {
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created LIKE '%".date('Y-m-d',strtotime("-1 day"))."%'";
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    } 
    public function getDaylast2()
    {
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created LIKE '%".date('Y-m-d',strtotime("-2 day"))."%'";
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    }  
    public function getDaylast3()
    {
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created LIKE '%".date('Y-m-d',strtotime("-3 day"))."%'";
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    }  
    public function getMonth()
    {
        $date = date("Y-m");
        $currMounth = date('Y-m', strtotime($date));
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created LIKE '%".$currMounth."%'";
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    } 
    public function getMonthlast1()
    {
        $date = date("Y-m");
        $currMounth = date('Y-m', (strtotime($date) - 2400000));
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created LIKE '%".$currMounth."%'";
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    } 
    public function getMonthlast2()
    {
        $date = date("Y-m");
        $currMounth = date('Y-m', (strtotime($date) - (2400000 * 2)));
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created LIKE '%".$currMounth."%'";
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    }   
    
    public function getWeek()
    {
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created BETWEEN '".date('Y-m-d', strtotime('-7 days'))."' AND '".date('Y-m-d',strtotime("+1 day"))."'";
        $db->setQuery($sql);    
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    }  
    public function getWeeklast1()
    {
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created BETWEEN '".date('Y-m-d', strtotime('-14 days'))."' AND '".date('Y-m-d', strtotime('-7 days'))."'";  
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    } 
    public function getWeeklast2()
    {
        $db = JFactory::getDbo();
        $sql="SELECT * FROM #__xplike_log WHERE created BETWEEN '".date('Y-m-d', strtotime('-21 days'))."' AND '".date('Y-m-d', strtotime('-14 days'))."'"; 
        $db->setQuery($sql); 
        $result=$db->loadObjectList();
        $data->like=0;                   
        $data->dislike=0;
        $contentarray= array();
        foreach ($result as $row)
        {
            if ($row->value == 1) $data->like++;
            else $data->dislike++;
        } 
        return $data;
    }  
    public function getComponent() {
        $db = JFactory::getDbo();
        $sql="SELECT component AS value, component AS text FROM #__xplike GROUP BY component"; 
        $db->setQuery($sql); 
        $result=$db->loadObjectList(); 
        return $result;       
    }   
    
    public function getUser() {
        $db = JFactory::getDbo();
        $date = date("Y-m");
        $currMounth = date('Y-m', strtotime($date));        
        $sql = "SELECT count(id) FROM #__xplike_log WHERE user_id <> 0 AND created LIKE '%".$currMounth."%'";
        $db->setQuery($sql);
        $result=$db->loadResult(); 
        return $result;         
    } 
    public function getGuest() {
        $db = JFactory::getDbo();
        $date = date("Y-m");
        $currMounth = date('Y-m', strtotime($date));        
        $sql = "SELECT count(id) FROM #__xplike_log WHERE user_id = 0 AND created LIKE '%".$currMounth."%'";
        $db->setQuery($sql);
        $result=$db->loadResult(); 
        return $result;         
    }                              
}